package interaction.input;

import interaction.data.DataSource;

import java.util.Map;

public class ComponentSwitcher<T> extends AbstractComponentDecorator {
	private Map<T, Component> components;
	private DataSource<T> selected = new DataSource<T>(null);
	
	public ComponentSwitcher(Map<T, Component> components, T selected)
	{
		this.components = components;
		setSelectedKey(selected);
	}
	
	public Component getInnerObject()
	{
		return getSelectedComponent();
	}
	
	public ComponentSwitcher<T> setSelectedKey(T selected)
	{
		if (getSelectedComponent() != null) {
			getSelectedComponent().setEnabled(false);
		}
		this.selected.set(selected);
		if (getSelectedComponent() != null) {
			getSelectedComponent().setEnabled(true);
		}
		return this;
	}
	
	public DataSource<T> getDataSource()
	{
		return selected;
	}
	
	public T getSelectedKey()
	{
		return selected.get();
	}
	
	public Component getSelectedComponent()
	{
		return components.get(getSelectedKey());
	}
	
	public Component getComponent(T key)
	{
		return components.get(key);
	}
	
	public ComponentSwitcher<T> setComponent(T key, Component d)
	{
		components.put(key, d);
		return this;
	}
	
}
